const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    trainDate: '',
    courseTrains: [{
      name: '胸部训练',
      selected: false,
      items: [
        '杠铃卧推', '上斜哑铃卧推', '坐姿器械推胸', '器械夹胸', '绳索飞鸟'
      ]
    }, {
      name: '背部训练',
      selected: false,
      items: [
        '俯身杠铃划船', '高位下拉', '器械坐姿划船', '直⽴下压', '哑铃单侧划船'
      ]
    }, {
      name: '手臂训练',
      selected: false,
      items: [
        '坐姿哑铃二头弯举', '曲干杠铃弯举', '牧师登器械弯举', '绳索下压', '俯身哑铃臂屈伸', '哑铃颈后臂屈伸'
      ]
    }, {
      name: '臀部训练',
      selected: false,
      items: [
        '相扑式深蹲', '负重臀桥', '哑铃深蹲', '器械蹲姿分腿', '史密斯架负重宽距深蹲'
      ]
    }, {
      name: '腹部训练',
      selected: false,
      items: [
        '仰卧卷腹', '仰卧举腿', '平板支撑', '哑铃负重侧屈', '轮流抬腿'
      ]
    }, {
      name: '腿部训练',
      selected: false,
      items: [
        '杠铃深蹲', '史密斯深蹲', '坐姿腿屈伸', '坐姿腿弯举', '直腿硬拉'
      ]
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const trainDate = wx.getStorageSync("trainDate");
    this.setData({
      trainDate
    });
  },
  changeSelect: function(e) {
    const index = e.currentTarget.dataset.index;
    const courseTrains = this.data.courseTrains;
    courseTrains[index].selected = !courseTrains[index].selected;
    this.setData({
      courseTrains: courseTrains
    });
  },
  addCourse: function() {
    const trainDate = this.data.trainDate;
    const courseTrains = this.data.courseTrains;
    const url = app.globalData.url;
    const openId = app.globalData.openId;
    const selectCourses = [];
    courseTrains.forEach(function(item) {
      if (item.selected) {
        selectCourses.push(item);
      }
    });
    wx.request({
      url: url + '/train/saveCourseTrains',
      data: {
        openId,
        trainDate,
        selectCourses
      },
      method: 'POST',
      success: function(response) {
        if (response.data.success) {
          wx.navigateBack({
            data: 1
          });
          wx.showToast({
            title: '添加成功',
            icon: 'none'
          })
        } else {
          wx.showToast({
            title: '添加课程失败:'+response.data.message,
            icon: 'none'
          })
        }
      }
    })
  }
})